import 'package:flutter/material.dart';
import 'package:flutter_question_app/controllers/question_controller.dart';
import 'package:get/get.dart';

import '../../../constants.dart';

class Option extends StatelessWidget {
  const Option({Key key, this.text, this.index, this.press}) : super(key: key);
  final String text;
  final int index;
  final VoidCallback press;

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: QuestionController(),
      builder: (QuestionController controller) {
        // 根当前答案获取 是否是正确项 初始化的时候，那几个值都是null
        Color getTheRightColor() {
          if (controller.isAnswered) {
            if (index == controller.correctAns) {
              return kGreenColor; //正确显示绿色
            } else if (index == controller.selectAns &&
                controller.selectAns != controller.correctAns) {
              return kRedColor; // 不正确显示红色
            }
          }
          return kGrayColor; // 其他的显示灰色
        }

        // 根据当前的选项 判断该显示那个图标

        IconData getTheRightIcon() {
          return getTheRightColor() == kRedColor ? Icons.close : Icons.done;
        }

        return InkWell(
          onTap: press,
          child: Container(
            margin: EdgeInsets.only(
              top: kDefaultPadding,
            ),
            padding: EdgeInsets.all(kDefaultPadding),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(15),
                border: Border.all(color: getTheRightColor())),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(
                  "${index + 1} .$text",
                  style: TextStyle(
                    color: getTheRightColor(),
                    fontSize: 16,
                  ),
                ),
                Container(
                  height: 26,
                  width: 26,
                  decoration: BoxDecoration(
                    color: getTheRightColor() == kGrayColor
                        ? Colors.transparent
                        : getTheRightColor(),
                    borderRadius: BorderRadius.circular(50),
                    border: Border.all(
                      color: getTheRightColor(),
                    ),
                  ),
                  child: getTheRightColor() == kGrayColor
                      ? null
                      : Icon(getTheRightIcon(), size: 16),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
